package com.bing.dao;

import com.bing.bean.weixin.Article;
import com.bing.bean.weixin.ResponseMessage;
import com.bing.dao.sqlProvider.MessageSQL;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Administrator on 2014/12/13.
 */

@Repository
public interface MessageDao {
    @InsertProvider(type = MessageSQL.class, method = "insert")
    int createMsg(ResponseMessage message);

    @Insert("insert into MessageArticle(msgId,articleId) values(#{message.id},#{article.id})")
    int addArticle(@Param("message") ResponseMessage message, @Param("article") Article article);

    @Select("select * from Message where id=#{id}")
    @Results(
            {@Result(property = "id", column = "id"),
                    @Result(property = "articles", column = "id",
                            many = @Many(select = "com.bing.dao.ArticleDao.getArticlesByMsgId")),
                    @Result(property = "image", column = "msgId",
                            one = @One(select = "com.bing.dao.MediaDao.findById"))
            }
    )
    ResponseMessage findById(int id);

    @Select("select m.* from Message m,AccountCommand ac where m.id = ac.msgId and ac.accountId=#{accountId} and ac.command=#{command}")
    @Results(
            {@Result(property = "id", column = "id"),
                    @Result(property = "articles", column = "id",
                            many = @Many(select = "com.bing.dao.ArticleDao.getArticlesByMsgId")),
                    @Result(property = "image", column = "msgId",
                            one = @One(select = "com.bing.dao.MediaDao.findById"))
            }
    )
    ResponseMessage findByAccountIdCommand(@Param("accountId") int accountId, @Param("command") String command);

    @Select("select m.* from Message m,AccountCommand ac where m.id = ac.msgId")
    @Results(
            {@Result(property = "id", column = "id"),
                    @Result(property = "articles", column = "id",
                            many = @Many(select = "com.bing.dao.ArticleDao.getArticlesByMsgId")),
                    @Result(property = "image", column = "msgId",
                            one = @One(select = "com.bing.dao.MediaDao.findById"))
            }
    )
    List<ResponseMessage> findByAccountId(int accountId);
}
